package com.mmg.cosplay.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.mmg.cosplay.entity.PuzzleCaptcha;
import com.mmg.cosplay.entity.R;
import com.mmg.cosplay.utils.CaptchaUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.Map;

/**
 * 滑块验证码
 */
@Tag(name = "滑块验证码管理")
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private CaptchaUtil captchaUtil;

    @Operation(summary = "获取滑块验证码")
    @GetMapping
    public ResponseEntity<?> captcha() {
        PuzzleCaptcha puzzleCaptcha = new PuzzleCaptcha(ResourceUtil.getStream("static/captcha/default.jpg"));
        puzzleCaptcha.setImageQuality(Image.SCALE_AREA_AVERAGING);
        puzzleCaptcha.run();
        return R.success(captchaUtil.captcha(puzzleCaptcha));
    }

    @Operation(summary = "验证滑块验证码")
    @PostMapping(value = "/verify")
    public ResponseEntity<?> verify(@RequestBody Map<String, Object> map) {
        return R.success(captchaUtil.verify(map));
    }
}
